import { createStore } from "vuex"
import { IRootState } from "@/types/userType"
import login from "./login/login"
import system from "./main/system/system"
import dashboard from "@/store/dashboard/dashboard"
import { getUserList } from "@/api/main/system/system"

const store = createStore<IRootState>({
  state() {
    return {
      name: "",
      password: "",
      breadcrumb: [],
      departmentList: [],
      rolesList: [],
    }
  },
  getters: {},
  mutations: {
    changeUserMenu(state, paylod: string[]) {
      state.breadcrumb = paylod
    },
    changeDepartmentList(state, payload: any[]) {
      state.departmentList = payload
    },
    changeRolesList(state, payload: any[]) {
      state.rolesList = payload
    },
  },
  actions: {
    // 获取部门列表
    async getDepartmentDate({ commit }) {
      const res = await getUserList({
        pageName: "department",
        queryInfo: {
          offset: 0,
          size: 100,
        },
      })
      console.log(res)
      commit("changeDepartmentList", res.list)
    },
    // 获取角色列表
    async getRoleDate({ commit }) {
      const res = await getUserList({
        pageName: "role",
        queryInfo: {
          offset: 0,
          size: 100,
        },
      })
      console.log(res)
      commit("changeRolesList", res.list)
    },
  },
  modules: {
    login,
    system,
    dashboard,
  },
})

export function setupStore() {
  store.dispatch("login/loadLocalLogin")
  store.dispatch("getDepartmentDate")
  store.dispatch("getRoleDate")
}

export default store
